package com.yunwaikeji.gy_tool_library.utils.picture.download;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;

import androidx.fragment.app.Fragment;

import com.yunwaikeji.gy_tool_library.utils.display.GYDisplayUtils;

import java.io.File;

public class GYPictureManager extends GYPictureBase {
    private Context context;
    private Activity activity;
    private Fragment fragment;
    private View view;
    private GYPictureModel pictureModel;
    private File filePath;

    public GYPictureManager(Context context) {
        this.context = context;
        filePath = new File(context.getExternalCacheDir(), "gy-picture-utils");
        if (!filePath.exists()) {
            if (!filePath.mkdirs()) {
                error("路径创建失败：" + filePath.getPath());
            }
        }
    }

    public GYPictureManager(Activity activity) {
        this.activity = activity;
    }

    public GYPictureManager(Fragment fragment) {
        this.fragment = fragment;
    }

    public GYPictureManager(View view) {
        this.view = view;
    }

//    public GYPictureBuilder<Bitmap> asBitmap() {
//        return new GYPictureBuilder<>(context, activity, fragment, view, pictureModel);
//    }
//
//    public GYPictureBuilder<Drawable> asDrawable() {
//        return new GYPictureBuilder<>(context, activity, fragment, view, pictureModel);
//    }
//
//    public GYPictureBuilder<File> asFile() {
//        return new GYPictureBuilder<>(context, activity, fragment, view, pictureModel);
//    }

    public GYPictureBuilder<Drawable> load(String url) {
        pictureModel = newPictureModel().setUrl(url);
        return new GYPictureBuilder<>(context, activity, fragment, view, pictureModel);
    }

    public GYPictureBuilder<Drawable> load(File file) {
        pictureModel = newPictureModel().setFile(file);
        return new GYPictureBuilder<>(context, activity, fragment, view, pictureModel);
    }

    public GYPictureBuilder<Drawable> load(Drawable drawable) {
        pictureModel = newPictureModel().setDrawable(drawable);
        return new GYPictureBuilder<>(context, activity, fragment, view, pictureModel);
    }

    public GYPictureBuilder<Drawable> load(Bitmap bitmap) {
        if (bitmap == null) {
            return new GYPictureBuilder<>(context, activity, fragment, view, pictureModel);
        }
        return load(GYDisplayUtils.toDrawable(bitmap));
    }

    public GYPictureBuilder<Drawable> load(Integer resId) {
        pictureModel = newPictureModel().setResId(resId);
        return new GYPictureBuilder<>(context, activity, fragment, view, pictureModel);
    }

    public GYPictureBuilder<Drawable> load(Uri uri) {
        pictureModel = newPictureModel().setUri(uri);
        return new GYPictureBuilder<>(context, activity, fragment, view, pictureModel);
    }

    private GYPictureModel newPictureModel() {
        return new GYPictureModel().setCachePath(filePath.getPath());
    }
}
